@echo off
echo Checking for required tools...

REM 检查是否已安装 Chocolatey
where choco >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo Installing Chocolatey...
    @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
    if %ERRORLEVEL% neq 0 (
        echo Failed to install Chocolatey
        pause
        exit /b 1
    )
)

REM 检查并安装 MinGW
where gcc >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo Installing MinGW...
    choco install mingw -y
    if %ERRORLEVEL% neq 0 (
        echo Failed to install MinGW
        pause
        exit /b 1
    )
)

REM 检查并安装 WinFlexBison
where win_flex >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo Installing WinFlexBison...
    choco install winflexbison3 -y
    if %ERRORLEVEL% neq 0 (
        echo Failed to install WinFlexBison
        pause
        exit /b 1
    )
)

echo All required tools are installed.

REM 编译过程
echo Starting compilation...

cd src

echo Generating lexer...
win_flex myfile.l

echo Generating parser...
win_bison -d myfile.y

echo Compiling...
gcc lex.yy.c myfile.tab.c -o compiler.exe

if %ERRORLEVEL% neq 0 (
    echo Compilation failed
    pause
    exit /b 1
)

echo Compilation successful! Created compiler.exe
pause 